package com.northwest_man.refactor_tool;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


// 复制Java文件中引用的Layout资源文件
public abstract class CopyLayoutInJava {
    private static final String module_name = "module_check_in";
    private static final String javaFileDir = "/Users/yong.hu/AndroidStudioProjects/patpat/" + module_name + "/src/main/java";


    private static final String from_module_root_dir = "/Users/yong.hu/AndroidStudioProjects/patpat/Patpat";
    private static final String to_module_root_dir = "/Users/yong.hu/AndroidStudioProjects/patpat/" + module_name;
    private static final String resource_dir = "/src/main/res";

    private static final String from_module_resource_dir = from_module_root_dir + resource_dir;
    private static final String to_module_resource_dir = to_module_root_dir + resource_dir;

    private enum ResourceType {
        layout("R\\.layout\\.([a-zA-Z0-9_]+)", from_module_resource_dir + "/layout", to_module_resource_dir + "/layout", "xml"),
        drawable_xml("R\\.drawable\\.([a-zA-Z0-9_]+)", from_module_resource_dir + "/drawable", to_module_resource_dir + "/drawable", "xml");
        public final String regex;
        public final String fromDir;
        public final String toDir;
        public final String suffix;

        ResourceType(final String regex,
                     final String fromDir,
                     final String toDir,
                     final String suffix) {
            this.regex = regex;
            this.fromDir = fromDir;
            this.toDir = toDir;
            this.suffix = suffix;
        }

        public String getFileAbsolutePath(final String fileName, final String type) {
            if (!"from".equals(type) && !"to".equals(type)) {
                throw new RuntimeException();
            }
            if ("from".equals(type)) {
                return this.fromDir + "/" + fileName + "." + this.suffix;
            } else {
                return this.toDir + "/" + fileName + "." + this.suffix;
            }

        }
    }

    public static void cp() {
        cp(javaFileDir);
    }

    private static void cp(final String javaFileDir) {
        final File javaFile = new File(javaFileDir);
        if (javaFile.isDirectory()) {
            final File[] allFile = javaFile.listFiles();
            if (allFile == null) return;
            for (final File item : allFile) {
                cp(item.getAbsolutePath());
            }

        } else {
            cp2(javaFile);
        }
    }

    private static void cp2(final File javaFile) {
        final String fileContent;
        try {
            fileContent = FileUtils.readFileToString(javaFile, Charset.defaultCharset());

        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        final StringBuilder stringBuilder = new StringBuilder();
        final ResourceType[] allResourceType = ResourceType.values();
        for (final ResourceType item : allResourceType) {
            final Pattern pattern = Pattern.compile(item.regex);
            final Matcher matcher = pattern.matcher(fileContent);
            while (matcher.find()) {
                final String resourceName = matcher.group(1);
                final String fromFileAbsolutePath = item.getFileAbsolutePath(resourceName, "from");
                try {
                    FileUtils.copyFile(new File(fromFileAbsolutePath), new File(item.getFileAbsolutePath(resourceName, "to")));
                } catch (IOException e) {
                    stringBuilder.append(fromFileAbsolutePath).append("\n");
                    e.printStackTrace();
                }
            }
        }


        if (stringBuilder.length() > 0) {

            System.out.println("以下文件复制失败：");
            System.out.println(stringBuilder);
        }
    }
}
